package com.azt.provider.impl;

import com.azt.api.pojo.Config;
import com.azt.api.service.ConfigService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.ConfigMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ConfigServiceImpl implements ConfigService
{
    
	@Autowired
	private ConfigMapper mapper;
    
    @Override
    public Pagination getConfigPage(int pageno, int pagesize, Map<String, String> searchMap)
    {
        Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		List<Config> list = mapper.getConfigByParams(searchMap);
		Pagination p = new Pagination(startPage, list);
		p.setList(list);
		p.setParams(searchMap);
        return p;
        
    }
    
    @Override
    public void saveConfig(Config config)
    {
    	mapper.insert(config);
    }
    
    @Override
    public void deleteAllById(Integer id)
    {
    	mapper.deleteByPrimaryKey(id);
    }
    
    @Override
    public void saveEdit(Config config)
    {
    	mapper.updateByPrimaryKey(config);
    }
    

    @Override
    public Config getConfigById(Integer id)
    {
        return mapper.selectByPrimaryKey(id);
    }
    
    @Override
    public String getConfigValueByCode(String code) {
        Config _config = new Config();
        _config.setCode(code);
        Config config = mapper.selectOne(_config);
        return config != null ? config.getValue() : null;
    }
}
